package org.itboys.core.service.mongo.web;

import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayCenterMessage;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/14
 * Description: 消息中心服务类
 */

@Service
public class SundayCenterMessageService extends BaseMongoService<SundayCenterMessage> {
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }
    @Override
    protected Class<SundayCenterMessage> getEntityClass() {
        return SundayCenterMessage.class;
    }

    /**
     * 分页查询所有消息
     * @param request
     * @return
     */
    public PageResult<SundayCenterMessage> select(HttpServletRequest request){
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        //等待加入条件
        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);

        PageResult<SundayCenterMessage> result = super.containpageQuery(request,vagueParam,param);

        return result;
    }

    /**
     * 新增或者更新消息
     * @param msgCenter
     */
    public void save(Long id, SundayCenterMessage msgCenter){
        if(LongUtil.isNotZreo(id)){
            super.updateExceptEmpty(id,msgCenter);
        }else {
            msgCenter.setStatus(0);
            msgCenter.setCreation(0);
            super.save(msgCenter);
        }
    }

    /**
     * 根据id查找消息中心
     * @param id
     * @return
     */
    public SundayCenterMessage findOne(Long id){
        SundayCenterMessage msgCenter = new SundayCenterMessage();
        if (LongUtil.isNotZreo(id)){
            return super.getById(id);
        }else return msgCenter;
    }

    /**
     * 设置状态
     * @param id
     * @param status
     */
    public void setStatus(Long id, Integer status) {
        super.update(id, "status", status);
    }

}
